﻿
Imports System.Net.Sockets
Imports System.Runtime.CompilerServices
Imports osi.root.utils
Imports osi.root.connector
Imports osi.service.device
Imports envs = osi.root.envs

Partial Public Class powerpoint
    Friend Class connection
        Public ReadOnly identity As String
        Private ReadOnly c As TcpClient
        Private lrm As Int64

        Public Shared Function validate(ByVal c As connection) As Boolean
            assert(Not c Is Nothing)
            Return c.no_refer_client().alive()
        End Function

        Public Shared Sub close(ByVal c As connection)
            assert(Not c Is Nothing)
            If envs.tcp_trace Then
                raise_error("connection ",
                            c.identity,
                            " has been closed by closer delegate")
            End If
            c.no_refer_client().shutdown()
        End Sub

        Public Sub New(ByVal c As TcpClient)
            assert(Not c Is Nothing)
            Me.c = c
            Me.identity = c.identity()
            update_refer_ms()
        End Sub

        Public Function client() As TcpClient
            update_refer_ms()
            Return c
        End Function

        Public Function last_refer_ms() As Int64
            Return lrm
        End Function

        'return the client without update refer time, use only in internal status check
        Public Function no_refer_client() As TcpClient
            Return c
        End Function

        Private Sub update_refer_ms()
            lrm = nowadays.milliseconds()
        End Sub
    End Class
End Class
